#!/usr/bin/perl
#vol_recovery.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $volname = $in{'volume'};
if ($volname eq "") {
	#volname can't be nulls	
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my (@rdinfo, @dinfo, @free_disks, $r, $rr);

@rdinfo = new_get_check_raid_info();
@dinfo = new_get_diskinfo(\@rdinfo);

my $found = 0;
foreach $r (@rdinfo) {
	if ($r->{'name'} eq $volname) {
		$rr = $r;
		$found = 1;
		last;
	}
}

if (!$found) {
	#finish latter
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

if (!new_can_recovery($rr)) {
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}
#-------------------------------------------
my @allraiddisks = &allraiddisks(@rdinfo);
my @alldisks = &alldisks(@dinfo);

my $d;
my $rd;
my @free_disks;
foreach $d (@alldisks){
        $found = 0;
        foreach $rd (@allraiddisks) {
                if($rd eq $d) {
                        $found = 1;
                        last;
                }
        }
        foreach $ad (@diskinfo) {
                if(($ad->{'device'} eq $d) && ($ad->{'spare'} eq "Y")){
                        $found = 1;
                        last;
                }
        }
        if(!$found){
                push(@free_disks, $d);
        }
}


#--------------------------------------------
#@free_disks = &new_get_free_disks(\@dinfo);
if (!@free_disks) {
	$err_msg = "ee".$text{"err_configchanged"};
	goto ERROR_OUT;
}

&header($text{'vol_recovery_title'}, "", "vol_recovery", 0, 1);
print "<form name=recoveryform action=save_recovery.cgi method=post onsubmit='return onsubmit_form();'>\n";

print "<table class=group width=100%>\n";
print "<tr> <td class=group> <table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";

#volume name
print "<tr> <td width=250> <b> $text{'rec_volname'} </b> </td>\n";
#print "<td width=200> $volname </td>\n";
print "<td width=200> $rr->{'volname'} </td>\n";
print "</tr> \n";

#free disk selection
print "<tr> <td width=250> <b> $text{'rec_adddisk'} </b> </td>\n";
print "<td width=200> <select name=adddisk class=drop> \n";
my ($fd);
foreach $fd (@free_disks) {
	if ($i == 0) {
		print "<option selected value=$fd> $fd \n";
	}
	else {
		print "<option value=$fd> $fd\n";
	}
	$i++;
}
print "</select> </td> </tr> \n";

print "</table> </td> </tr> \n";
#recovery button
print "<tr> <td class=group> <table class=mode1 border=0 cellpadding=3 cellspacing=0> \n";
print "<tr> <td> <input type=submit name=recoveryvolume value=\"$text{'rec_recovery'}\" class=button> </td> </tr>\n";
print "</table> </td> </tr> \n";

print "</table>\n";

print "<input type=hidden name=volname value=$volname>\n";
print "</form>\n";

print <<EOF;
<script>
function onsubmit_form()
{
        document.forms[0].recoveryvolume.disabled = true;
}

</script>
EOF

&footer("index.cgi", $text{'index'});
return;
	
ERROR_OUT:
&error_setup($text{'rec_err_title'});
&error($err_msg);

